package Q8_02_Robot_in_a_Grid; import java.util.ArrayList; import CtCILibrary.AssortedMethods; public class QuestionA { public static ArrayList<Point> getPath(boolean[][] maze) { if (maze == null || maze.length == 0) return null; ArrayList<Point> path = new ArrayList<Point>(); if (getPath(maze, maze.length - 1, maze[0].length - 1, path)) { return path; } return null; } public static boolean getPath(boolean[][] maze, int row, int col, ArrayList<Point> path) { // If out of bounds or not available, return. if (col < 0 || row < 0 || !maze[row][col]) { return false; } boolean isAtOrigin = (row == 0) && (col == 0); // If there's a path from the start to my current location, add my location. if (isAtOrigin || getPath(maze, row, col - 1, path) || getPath(maze, row - 1, col, path)) { Point p = new Point(row, col); path.add(p); return true; } return false; } public static void main(String[] args) { int size = 5; boolean[][] maze = AssortedMethods.randomBooleanMatrix(size, size, 70); AssortedMethods.printMatrix(maze); ArrayList<Point> path = getPath(maze); if (path != null) { System.out.println(path.toString()); } else { System.out.println("No path found."); } } }